package com.pj.point.util.net;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttp {
    static String URL = "";
    public static OkHttpClient client = new OkHttpClient();
    public static void get(String url,Then then){
        Request request = new Request.Builder()
                .url(URL + url)
                .get()
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败！");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                then.then(response.body().string());
            }
        });
    }

    public static void put(String url,String json,Then then){
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        Request request = new Request.Builder()
                .url(URL + url)
                .put(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败！");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                then.then(response.body().string());
            }
        });
    }
}
